<?php
final class HouseKeepingMessageModel {
	private $mysql;
	private $tableName = 'HouseKeepingMessage';
	public $idRowName = 'HouseKeepingMessage_ID';
	public $messageidRowName = 'HouseKeepingPresetMessage_ID';
	public $circleIDRowName = 'From_Circle_ID';
	public $pubDateRowName = 'HouseKeepingMessage_pubDate';
	public $descriptionRowName = 'HouseKeepingMessage_description';
	public $isCompleteRowName = 'HouseKeepingMessage_IsComplete';
	public $completeDateTimeRowName = 'HouseKeepingMessage_CompleteDateTime';
//	public $qtyRowName = 'HouseKeepingMessage_Qty';
	public $deviceIDRowName = 'Complete_Device_ID';
	public $CountRowName = 'HouseKeepingMessage_Count';
	public $RequestRowName = 'HouseKeepingMessage_Request';

	public function __construct ($mysql) {
		$this->mysql = $mysql;
	}
/*
	public function GetGuestBookCount () {
		$ref = 0;
		$sqlresult = $this->mysql->query (sprintf ('SELECT COUNT(*) AS `%s` FROM `%s`;', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName)));
		$len = $sqlresult->num_rows ();
		if ($len > 0) {
			$data = $sqlresult->fetch_array ();
			$ref = $data[$this->CountRowName];
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function GetGuestBookItems ($offset, $count) {
		$ref = array ();
		$sqlresult = $this->mysql->query (sprintf ('SELECT `%s`, `%s`, `%s` FROM `%s` ORDER BY `%s` DESC LIMIT %d, %d;', $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($offset), $this->mysql->escape_string ($count)));
		while ($data = $sqlresult->fetch_array HouseKeepingMessage_CompleteDateTime()) {
			$ref[] = $data;
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function DelGuestBookItem ($id) {
		$len = 0;
		$sqlresult = $this->mysql->query (sprintf ('DELETE FROM `%s` WHERE `%s`=\'%s\';', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($id)));
		$len = $sqlresult->affected_rows ();
		$sqlresult->free_result ();
		return $len;
	}

	public function AddGuestBookItem ($description) {
		$sqlresult = $this->mysql->query (sprintf ('INSERT INTO `%s` (`%s`, `%s`) VALUES (now(), \'%s\');', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string ($description)));
		$sqlresult->free_result ();
	}
*/

	public function AddHouseKeepingMessage ($description, $From, $request, $id) {
		$sqlresult = $this->mysql->query (sprintf ('INSERT INTO `%s` (`%s`, `%s`, `%s`, `%s`, `%s`) VALUES (%d, now(), \'%s\', \'%s\', \'%s\');', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->circleIDRowName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($this->descriptionRowName), $this->mysql->escape_string($this->RequestRowName), $this->mysql->escape_string($this->messageidRowName), $this->mysql->escape_string ($From), $this->mysql->escape_string ($description), $this->mysql->escape_string($request), $this->mysql->escape_string($id)));
		$sqlresult->free_result ();
	}

	public function SetToComplete ($messageID, $DeviceID) {
		$sqlresult = $this->mysql->query (sprintf ('UPDATE `%s` SET `%s`=%d, `%s`=NOW(), `%s`=1 WHERE `%s`=%d;', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->deviceIDRowName), $this->mysql->escape_string ($DeviceID), $this->mysql->escape_string ($this->completeDateTimeRowName), $this->mysql->escape_string ($this->isCompleteRowName), $this->mysql->escape_string ($this->idRowName), $this->mysql->escape_string ($messageID)));
		$sqlresult->free_result ();
	}

	public function CountUNCompleteWithin24Hours () {
		$ref = array ();
		$sql = sprintf ('SELECT * FROM `%s` WHERE `%s` >= now() - INTERVAL 1 DAY AND `%s`=0;', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($this->isCompleteRowName));
		$sqlresult = $this->mysql->query ($sql);
		while ($data = $sqlresult->fetch_array ()) {
			$ref[] = $data;
		}
		$sqlresult->free_result ();
		return $ref;
	}

	public function CountMessageWithin24Hours () {
		$ref = 0;
		$sql = sprintf ('SELECT COUNT(*) AS `%s` FROM `%s` WHERE `%s` >= now() - INTERVAL 1 DAY;', $this->mysql->escape_string ($this->CountRowName), $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->pubDateRowName));
		$sqlresult = $this->mysql->query ($sql);
		$len = $sqlresult->num_rows ();
		if ($len > 0) {
			$data = $sqlresult->fetch_array ();
			$ref = $data[$this->CountRowName];
		}
		$sqlresult->free_result ();
		return $ref;
	}


	public function GetHouseKeepingMessages ($offset, $count) {
		$ref = array ();
//		$sql = sprintf ('SELECT * FROM `%s` ORDER BY `%s`, `%s` LIMIT %d, %d;', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->isCompleteRowName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($offset), $this->mysql->escape_string ($count));
		$sql = sprintf ('SELECT * FROM `%s` WHERE `%s`=0 ORDER BY `%s` LIMIT %d, %d;', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->isCompleteRowName), $this->mysql->escape_string ($this->pubDateRowName), $this->mysql->escape_string ($offset), $this->mysql->escape_string ($count));
		$sqlresult = $this->mysql->query ($sql);
		while ($data = $sqlresult->fetch_array ()) {
			$ref[] = $data;
		}
		$sqlresult->free_result ();

		$sql = sprintf ('SELECT * FROM `%s` WHERE `%s`=1 ORDER BY `%s` DESC LIMIT %d, %d;', $this->mysql->escape_string ($this->tableName), $this->mysql->escape_string ($this->isCompleteRowName), $this->mysql->escape_string ($this->completeDateTimeRowName), $this->mysql->escape_string ($offset), $this->mysql->escape_string ($count - count($ref)));
		$sqlresult = $this->mysql->query ($sql);
		while ($data = $sqlresult->fetch_array ()) {
			$ref[] = $data;
		}
		$sqlresult->free_result ();

		return $ref;
	}
}
?>
